import {
  Controller,
  Post,
  Body,
  HttpCode,
  HttpStatus,
  UseGuards,
  Get,
  Req,
} from '@nestjs/common';
import { AuthorizeService } from './authorize.service';
import { LoginUserDto } from './dto/login-user.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthorizeGuard } from './authorize.guard';
import { Request } from 'express';
import { VerifyCodeDto } from './dto/verify-code.dto';

@ApiTags('权限')
@Controller('authorize')
export class AuthorizeController {
  constructor(private readonly authorizeService: AuthorizeService) {}

  @ApiOperation({ summary: '用户名密码登录' })
  @HttpCode(HttpStatus.OK)
  @Post('login')
  login(@Body() user: LoginUserDto) {
    return this.authorizeService.login(user);
  }

  @ApiOperation({ summary: '退出登录' })
  @HttpCode(HttpStatus.OK)
  @Post('logout')
  logout(@Req() request: Request) {
    return this.authorizeService.logout(request.headers.authorization);
  }

  @ApiOperation({ summary: '获取手机/邮箱验证码' })
  @HttpCode(HttpStatus.OK)
  @Post('getVerifyCode')
  getVerifyCode(@Body() verify: VerifyCodeDto) {
    console.info('getVerifyCode', verify);
    return this.authorizeService.getVerifyCode(verify);
  }

  @ApiOperation({ summary: '测试Guard' })
  @UseGuards(AuthorizeGuard)
  @Get('profile')
  profile(@Req() req: Request) {
    console.info(req);
    return 'profile';
  }
}
